package com.group.eams.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.group.eams.pojo.dto.PatentCategoryQueryDto;
import com.group.eams.pojo.entity.PatentCategory;
import com.group.eams.service.PatentCategoryService;
import com.group.pojo.dto.BatchDeleteDto;
import com.group.result.Result;
import com.group.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 专利分类表(PatentCategory)表控制层
 *
 * @author DHUser
 * @since 2024-10-30 12:58:28
 */
@RestController
@RequestMapping("/patentCategory")
public class PatentCategoryController  {
    /**
     * 服务对象
     */
    @Autowired
    private PatentCategoryService patentCategoryService;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @RequestMapping(value = "/find",method = RequestMethod.GET)
    public Result findById(@RequestParam Long id){
        PatentCategory patentCategory = patentCategoryService.getById(id);
        return Result.buildSuccess(patentCategory);
    }

    /**
     * 分页查询
     *
     * @param patentCategoryQueryDto 筛选条件
     * @return 查询结果
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result list(PatentCategoryQueryDto patentCategoryQueryDto){
        Page<PatentCategory> page = patentCategoryService.findByQueryDto(patentCategoryQueryDto);
        return Result.buildSuccess(page.getTotal(),page.getRecords());
    }

    /**
     * 查询所有数据
     *
     * @return 所有数据
     */
    @RequestMapping(value = "/findAll",method = RequestMethod.GET)
    public Result findAll(){
        List<PatentCategory> list = patentCategoryService.list();
        return Result.buildSuccess(list);
    }

    /**
     * 新增数据
     * @param patentCategory 实体
     * @return 新增结果
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Result add(@RequestBody PatentCategory patentCategory){
        boolean result = patentCategoryService.save(patentCategory);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量新增数据
     * @param patentCategoryList 实体集合
     * @return 新增结果
     */
    @RequestMapping(value = "/addBatch",method = RequestMethod.POST)
    public Result addBatch(@RequestBody List<PatentCategory> patentCategoryList){
        boolean result = patentCategoryService.saveBatch(patentCategoryList);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 修改数据
     * @param patentCategory 实体
     * @return 修改结果
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Result update(@RequestBody PatentCategory patentCategory){
        boolean result = patentCategoryService.updateById(patentCategory);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量修改数据
     * @param patentCategoryList 实体集合
     * @return 修改结果
     */
    @RequestMapping(value = "/updateBatch",method = RequestMethod.POST)
    public Result updateBatch(@RequestBody List<PatentCategory> patentCategoryList){
        boolean result = patentCategoryService.updateBatchById(patentCategoryList);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @RequestMapping(value = "/delete",method = RequestMethod.GET)
    public Result delete(@RequestParam Long id){
        boolean result = patentCategoryService.removeById(id);
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

    /**
     * 批量删除数据
     *
     * @param batchDeleteDto 实体
     * @return 删除是否成功
     */
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.POST)
    public Result deleteBatch(@RequestBody BatchDeleteDto batchDeleteDto){
        boolean result = patentCategoryService.removeByIds(batchDeleteDto.getIds());
        return result ? Result.buildSuccess() : Result.buildFail(ResultEnum.OPTION_FAIL);
    }

}

